import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:quick_code_local/routes/pages.dart';
import 'package:quick_code_platform/constant/constant.dart';
import 'package:glorysoft_mobile_plugin/tool/extensions/extension.dart';
import 'package:glorysoft_mobile_plugin/tool/service/app_package_service.dart';
import 'package:glorysoft_mobile_plugin/tool/service/app_sp_service.dart';
import 'package:quick_code_platform/constant/quick_initial.dart';
import 'package:quick_code_platform/entity/enum_entity.dart';
import 'package:quick_code_platform/http/http_service.dart';
import 'package:glorysoft_mobile_plugin/tool/utils/shared_preferences_util.dart';
import 'package:glorysoft_mobile_plugin/tool/global/global_method.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);
  await initServices();

  if(!(SpUtil.containsKey('quick_address') ?? false)){//文件地址
    SpUtil.putString('quick_address', 'http://192.168.0.181:8033');
  }
  if(!(SpUtil.containsKey('quick_timeout') ?? false)){//超时时间
    SpUtil.putString('quick_timeout', '30');
  }
  if(!(SpUtil.containsKey('interface_address') ?? false)){//接口地址
    SpUtil.putString('interface_address', 'http://192.168.0.181:5056');
  }
  if(!(SpUtil.containsKey('quick_catalogue') ?? false)) {//文件目录
    SpUtil.putString('quick_catalogue', '');
  }
  if(!(SpUtil.containsKey('isFailingDialog') ?? false)) {
    SpUtil.putBool('isFailingDialog', false);
  }else{
    isHttpFailingModusOrDialog = SpUtil.getBool('isFailingDialog') ?? false;
  }
  initialQuickConfig(
      modeType: RunModeType.local);
  runApp( getMainApp());
}
initServices() async {
  await Get.putAsync(() => AppSpService().init());
  await Get.putAsync(() => AppPackageService().init());
  await Get.putAsync(() => HttpService().init());
}
getMainApp() {
  return ScreenUtilInit(
    designSize: const Size(360, 640),
    builder: (context , child) => KeyboardDismissOnTap(
        child:  RefreshConfiguration(
          headerBuilder: () => const ClassicHeader(),        // 配置默认头部指示器,假如你每个页面的头部指示器都一样的话,你需要设置这个
          footerBuilder:  () => const ClassicFooter(),        // 配置默认底部指示器
          headerTriggerDistance: 80.0,        // 头部触发刷新的越界距离
          maxOverScrollExtent :100, //头部最大可以拖动的范围,如果发生冲出视图范围区域,请设置这个属性
          maxUnderScrollExtent:0, // 底部最大可以拖动的范围
          enableScrollWhenRefreshCompleted: true, //这个属性不兼容PageView和TabBarView,如果你特别需要TabBarView左右滑动,你需要把它设置为true
          enableLoadingWhenFailed : true, //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
          hideFooterWhenNotFull: false, // Viewport不满一屏时,禁用上拉加载更多功能
          enableBallisticLoad: true,
          child: GetMaterialApp(
              localizationsDelegates: const [
                // 这行是关键
                RefreshLocalizations.delegate,
                GlobalMaterialLocalizations.delegate, // 指定本地化的字符串和一些其他的值
                GlobalCupertinoLocalizations.delegate, // 对应的Cupertino风格
                GlobalWidgetsLocalizations.delegate // 指定默认的文本排列方向, 由左到右或由右到左
              ],
              supportedLocales: const [
                Locale('en'),
                Locale('zh'),
              ],
              debugShowCheckedModeBanner: false,
              initialRoute: Pages.initial,
              getPages: Pages.routes,
              theme: ThemeData(
                  appBarTheme: AppBarTheme(
                      backgroundColor: middle_white.toColor(), centerTitle: true)),
              builder: EasyLoading.init()),
        )
    ),
  );
}
